home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / JOYPROG.ZIP;1 / JOYSTICK.TXT
Encoding:
Text File  |  1994-07-14  |  9.9 KB  |  252 lines

  1.                     ⁄ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒø
  2.                     ≥ Programming the PC Joystick  ≥
  3.                     ¿ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒŸ
  4.  
  5. ⁄ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ¬ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
  6. ≥ Programming Info ≥
  7. ¿ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒŸ
  8.  
  9. All joystick programming is done via port 201h.
  10.  
  11.                       ⁄ƒƒƒ¬ƒƒƒ¬ƒƒƒ¬ƒƒƒ¬ƒƒƒ¬ƒƒƒ¬ƒƒƒ¬ƒƒƒø
  12.                       ≥ 7 ≥ 6 ≥ 5 ≥ 4 ≥ 3 ≥ 2 ≥ 1 ≥ 0 ≥
  13.                       ¿ƒƒƒ¡ƒƒƒ¡ƒƒƒ¡ƒƒƒ¡ƒƒƒ¡ƒƒƒ¡ƒƒƒ¡ƒƒƒŸ
  14.                         ≥   ≥   ≥   ≥   ≥   ≥   ≥   ≥
  15. Joystick B, Button 2 ƒƒƒŸ   ≥   ≥   ≥   ≥   ≥   ≥   ¿ƒƒƒ Joystick A, X Axis
  16. Joystick B, Button 1 ƒƒƒƒƒƒƒŸ   ≥   ≥   ≥   ≥   ¿ƒƒƒƒƒƒƒ Joystick A, Y Axis
  17. Joystick A, Button 2 ƒƒƒƒƒƒƒƒƒƒƒŸ   ≥   ≥   ¿ƒƒƒƒƒƒƒƒƒƒƒ Joystick B, X Axis
  18. Joystick A, Button 1 ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒŸ   ¿ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ Joystick B, Y Axis
  19.  
  20. Reading the status of the joystick buttons is fairly simple. Just read the
  21. byte from the joystick port and check the status of the appropriate bit. A
  22. clear bit (0) means the button is pressed, a set bit (1) means it is not
  23. pressed. Note that the button's are not hardware debounced. Each time a
  24. button is pressed it's bit may "bounce" between 0 and 1 a couple of times.
  25.  
  26. Reading the position of the stick positions is a bit more complicated. You
  27. must first write a dummy byte (any value will do) to the joystick port. This
  28. will set each axis bit to 1. You must then time how long the bit takes to
  29. drop back to 0, this time is roughly proportional to the position of
  30. the joystick axis (see Steve McGowan's discussion below).
  31.  
  32. AT computers also have a BIOS call which supports the joystick. I have come
  33. across numerous machines which apparently did not support this call. My own
  34. machine supports reading the joystick buttons apparently can't read the
  35. stick position values, so I do not advise using this call for any serious
  36. games. In any case here is info on the call:
  37.  
  38. Joystick Support BIOS Call
  39.  
  40. Int 15h
  41.  
  42. To call:
  43.   AH = 84h
  44.   DX = 00h Read switch settings
  45.        01h Read joystick position
  46.  
  47. Returns:
  48.     PC, PCjr : Carry flag set, AH = 80h
  49.        PC XT : Carry flag set, AH = 86h
  50.   All others : DX = 00h on calling
  51.                  AL = Switch settings (bits 4 - 7)
  52.                  Carry flag set on error
  53.                DX = 01h on calling
  54.                  AX = A(X) value
  55.                  BX = A(Y) value
  56.                  CX = B(X) value
  57.                  DX = B(Y) value
  58.  
  59. ⁄ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ¬ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
  60. ≥ Hardware Pinout ≥
  61. ¿ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒŸ
  62.  
  63. The joystick connects to a 15 pin female plug :
  64.  
  65.                      __________________________
  66.                      \ 8  7  6  5  4  3  2  1 /
  67.                       \ 9  10 11 12 13 14 15 /
  68.                        ----------------------
  69.  
  70.                   ⁄ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒø
  71.                   ≥ Pin #  Joystick               ≥
  72.                   √ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ¥
  73.                   ≥  1     +5v                    ≥
  74.                   ≥  2     Joystick A, Button 1   ≥
  75.                   ≥  3     Joystick A, X Axis     ≥
  76.                   ≥  4     Gnd                    ≥
  77.                   ≥  5     Gnd                    ≥
  78.                   ≥  6     Joystick A, Y Axis     ≥
  79.                   ≥  7     Joystick A, Button 2   ≥
  80.                   ≥  8     +5v                    ≥
  81.                   ≥  9     +5v                    ≥
  82.                   ≥  10    Joystick B, Button 1   ≥
  83.                   ≥  11    Joystick B, X Axis     ≥
  84.                   ≥  12    Gnd                    ≥
  85.                   ≥  13    Joystick B, Y Axis     ≥
  86.                   ≥  14    Joystick B, Button 2   ≥
  87.                   ≥  15    +5v                    ≥
  88.                   ¿ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒŸ
  89.  
  90.  
  91. ⁄ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ¬ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
  92. ≥ Misc notes on Joystick handling by Steve McGowan ≥
  93. ¿ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒŸ
  94.  
  95. With a polling loop on a 486-66 I got x/y values between 8 and 980. When
  96. I centered the stick the value was usually a value around 330.
  97.  
  98. NOTE: a Gravis Game Pad it only put out 3 values, 8(min), 330(center),
  99. and 980(max). Every joystick I have tried has been non-linear.
  100.  
  101. The "speed compensation" that some games require is due to the fact that
  102. the game designer did not anticipate the range of values that could
  103. come back on faster machines. On a 486-25 you may see max values of 360,
  104. I saw 980, on a Pentium the max value could be well over 2000. If you
  105. had used a unsigned byte value you probably would have been in good
  106. shape on an AT, or 386 but you would be in big trouble with faster machines.
  107.  
  108. Because the joystick logic returns a non linear value, if you base your
  109. scaling only on the 4 corners then the center will be off (biased towards
  110. a corner). If you just use the center value and a single scaling factor
  111. (i.e. of the center is at 330 then full throw should be at 660), then the
  112. stick will saturate (660) half way to the full throw position (980).
  113. That is why most joystick setup programs make the distinction between
  114. hitting the 4 corners and centering the stick.
  115.  
  116. Joystick position vs. loop count
  117.  
  118.      x,y--------------------
  119.      8,8|      330,8       | 980,8
  120.         |                  |
  121.         |                  |    delta 330
  122.         |                  |
  123.    8,330|      330,330     | 980,330 (y centered)
  124.         |                  |
  125.         |                  |    delta 650
  126.         |                  |
  127.    8,980|      330,980     | 980,980
  128.         --------------------
  129.             (x centered)
  130.  
  131. For the best effect you basically need 2 scale factors, depending on whether
  132. you are above or below the center value. I think the curve is actually an
  133. exponential (charging capacitor) but a straight line approximation should
  134. do fine.
  135.  
  136. The 10% dead zone in the center is a good idea. The centering mechanism of
  137. joysticks vary in repeatablity, they don't always come back to the same place.
  138. I have a cheap one that (1 time in 8) does not return to the X center if I
  139. just let it snap to center. It hangs on the high side.
  140.  
  141. I would recommend disabling interrupts while polling. An interrupt
  142. in the middle of your polling loop will really throw off the results. And
  143. any DMA that takes place will also give you bad values.
  144.  
  145. Joysticks are noisy, so holding the stick in a fixed position will return
  146. values that vary +-5% easily. I added a smoothing function to my joystick
  147. code where I throw away single values that are not continuous. It helped
  148. a lot with the noise and the DMA.
  149.  
  150. I use protected mode and the interrupt disable() call doesn't actually work
  151. because it only disables interrupts for the process not the processor.
  152. The smoothing trick can help here too.
  153.  
  154. If I turn on my machine and start the polling loop immediately, it will
  155. put out a centered value of 330,330 but after warming up for 10 minutes
  156. the value changes to 285,285. This variance also needs to be absorbed in
  157. your center dead zone. If after warming up the 'center' value is outside your
  158. dead zone then the cursor will drift (to the left and/or up). Make
  159. sure your game has a "center joystick" command to get around joystick
  160. interfaces with lousy temperature compensation.
  161.  
  162. You must wait for all of the axis bits to settle before initiating
  163. another read, otherwise strange results may come out. So, instead of
  164. reading X, then Y, in two separate loops (which take twice as much time)
  165. Read both X and Y simultaneously, polling until both bits settle. This
  166. can be extended for two joysticks, assuming that they are both attached.
  167. The respective X/Y bits never come true if there is no joystick attached.
  168.  
  169.  
  170. ⁄ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ¬ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
  171. ≥ A Simple Demo Joystick Unit ≥
  172. ¿ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒŸ
  173.  
  174. {
  175.   JOY.PAS - By Mark Feldman
  176.             e-mail address : u914097@student.canberra.edu.au
  177.                              myndale@cairo.anu.edu.au
  178.  
  179.  
  180.   A simple Pascal Joystick Unit.
  181. }
  182.  
  183.  
  184. unit Joy;
  185.  
  186. Interface
  187.  
  188. { Define constants for use as JoystickButton and JoystickPosition parameters }
  189. const JoystickAButton1 = $10;
  190.       JoystickAButton2 = $20;
  191.       JoystickBButton1 = $40;
  192.       JoystickBButton2 = $80;
  193.       JoystickAAxisX   = $01;
  194.       JoystickAAxisY   = $02;
  195.       JoystickBAxisX   = $04;
  196.       JoystickBAxisY   = $08;
  197.  
  198. function JoystickButton(buttonnum : byte) : boolean;
  199. function JoystickPosition(axisnum : byte) : word;
  200.  
  201. Implementation
  202.  
  203. const JOYSTICKPORT = $201;
  204.  
  205. { Button returns true is button is pressed }
  206. function JoystickButton(buttonnum : byte) : boolean;
  207. begin
  208.   JoystickButton := (Port[JOYSTICKPORT] and buttonnum) = 0;
  209. end;
  210.  
  211. { Returns position value of joystick. The value returned is highly
  212.   dependent on machine speed. Changing the setting of the computer's
  213.   Turbo speed button will affect the value returned.
  214.   Returns $FFFF if the joystick is not connected
  215. }
  216. function JoystickPosition(axisnum : byte) : word;
  217. var count : word;
  218. begin
  219.   asm
  220.     mov word ptr count, 0
  221.     cli          { Disable interrupts so they don't interfere with timing }
  222.     mov dx, JOYSTICKPORT   { Write dummy byte to joystick port }
  223.     out dx, al
  224.     @joystickloop:
  225.     inc count              { Add one to count }
  226.     cmp count, $FFFF       { Check for time out }
  227.     je @done
  228.     in al, dx              { Get joystick port value }
  229.     and al, axisnum        { Test the appropriate bit }
  230.     jne @joystickloop
  231.     @done:
  232.     sti                    { Enable interrupts again }
  233.   end;
  234.   JoystickPosition := count;
  235. end;
  236.  
  237. end.
  238.  
  239.  
  240. ⁄ƒƒƒƒƒƒƒƒƒƒƒƒƒ¬ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
  241. ≥ References  ≥
  242. ¿ƒƒƒƒƒƒƒƒƒƒƒƒƒŸ
  243.  
  244. Title : Flights of Fantasy
  245. Author : Christopher Lampton
  246. Publishers : The Waite Group
  247. ISBN : 1-878739-18-2
  248.  
  249. Title : DOS and BIOS Functions Quick Reference
  250. Publishers : Que Corporation
  251. ISBN : 0-88022-426-6
  252.